package lk.icta.core.web.action;

import lk.icta.common.AppConstants;
import lk.icta.common.web.action.BaseAction;
import lk.icta.core.entity.LandApplication;
import lk.icta.core.entity.ModuleType;
import lk.icta.core.entity.RegisteredPost;
import lk.icta.core.entity.WorkflowTask;
import lk.icta.core.service.LandApplicationManagementService;
import lk.icta.core.service.RegisteredPostManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kusal Samarakoon
 */
public class RegisteredPostAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(RegisteredPostAction.class);

    private final RegisteredPostManagementService registeredPostManagementService;
    private final LandApplicationManagementService landApplicationManagementService;

    private RegisteredPost registeredPost;
    private long moduleId;


    public RegisteredPostAction(RegisteredPostManagementService registeredPostManagementService,
                                LandApplicationManagementService landApplicationManagementService) {
        this.registeredPostManagementService = registeredPostManagementService;
        this.landApplicationManagementService = landApplicationManagementService;

    }

    public String welcome() {
        logger.debug("Welcome RegisteredPostAction");
        return SUCCESS;
    }

    //loadLetterDetail
    public String loadLetterDetail() {
        try {
            logger.debug("load Letter Detail {} - ", user.getUsername());
            return SUCCESS;
        } catch (Exception e) {
            logger.error("Error in  load Letter Detail  : {}", e);
            e.printStackTrace();
            return ERROR;
        }

    }

    //save Register Letter Detail
    public String addRegLetterDetail() {
        try {
            logger.debug("save Register Letter Detail {} - ", user.getUsername());
            logger.debug("save Register Letter Date {} - ", registeredPost.getRegisteredDate());
            logger.debug("save Register Letter RefNo {} - ", registeredPost.getRegisteredPostRefNumber());
            logger.debug("save Register Letter RefNo {} - ", moduleId);

            if (moduleId != -1) {

                registeredPost.setModuleId(moduleId);
                if (AppConstants.LO_USER_ROLE.equals(user.getRole().getId())) {

                    LandApplication landApplication;
                    landApplication = landApplicationManagementService.getLandApplicationByIdUKey(moduleId, user);
                    if (landApplication.getInitialStatus() == LandApplication.State.INITIAL_APPLICATION_APPROVED ||
                            landApplication.getInitialStatus() == LandApplication.State.INITIAL_APPLICATION_REJECTED) {
                        registeredPost.setModuleType(ModuleType.INITIAL_APP);

                    } else if (landApplication.getWorkflowItem().getWorkflowTask() == WorkflowTask.LAND_ALLOCATION_APPROVED) {
                        registeredPost.setModuleType(ModuleType.LAND_ALLOCATION_APPROVED);

                    }

                } else if (AppConstants.ORMOI_USER_ROLE.equals(user.getRole().getId())) {
                    registeredPost.setModuleType(ModuleType.LAND_INTEREST);
                }


                registeredPostManagementService.addRegisteredPost(registeredPost, user);
                addActionMessage(getText("successfuly_save_reg_letter"));
            } else {
                addActionError(getText("unable_to_save_reg_letter"));
            }
            return SUCCESS;
        } catch (Exception e) {
            logger.error("Error in save Register Letter Detail  : {}", e);
            e.printStackTrace();
            addActionError(getText("unable_to_save_reg_letter"));
            return ERROR;
        }

    }

    public RegisteredPost getRegisteredPost() {
        return registeredPost;
    }

    public void setRegisteredPost(RegisteredPost registeredPost) {
        this.registeredPost = registeredPost;
    }


    public long getModuleId() {
        return moduleId;
    }

    public void setModuleId(long moduleId) {
        this.moduleId = moduleId;
    }
}
